<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.*"%>
<%@ page import="forum.*"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String username = (String) request.getSession().getAttribute(
			"username");
	Boolean isModerator = (Boolean) request.getSession().getAttribute(
			"isModerator");
	int forumID = ((Integer)request.getSession().getAttribute("forumID")).intValue();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
	var int = self.setInterval("checkNotifications()", 10000);

	function checkNotifications() {
		check();
	}
	
	function check(){
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", "viewThreadsProcessor?do=check", true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var friend = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("friend")[0].firstChild.data;
					if (friend == "y")
						alert("Your friend added post");
					var author = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("author")[0].firstChild.data;
					if (author == "y")
						alert("Post added to your thread");
				} else
					alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
			}
		};
		xmlhttp.send(null);
	}
	
	function showThread(id) {
		var xmlhttp;

		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.open("GET", "viewThreadsProcessor?do=ajaxShow&id=" + id, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					//process the response
					var mdiv = document.getElementById("thread"+id);
					mdiv.innerHTML = xmlhttp.responseText;
				} else
					alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
			}
		};
		xmlhttp.send(null);
	}
	
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<base href="<%=basePath%>">
<title>Threads list</title>
</head>
<body background="background.jpg" bgcolor="AliceBlue" link="Navy" text="Navy" >
<font face="COMIC SANS MS" >

	<h1 align="center">
		<font size="10"  color="FloralWhite">Threads list</font>
	</h1>
	
	<%
		List<forum.Thread> threadsList = (ArrayList) request
				.getAttribute("threads");
		for (forum.Thread t : threadsList) {
	%>
	<hr>
		<p align="center">
			<br>
			<%=t.getAuthor()%>
			posted at
			<%=t.getRoot().getDate()%>
			<br> <font size="5" ><b>Subject: </b></font><%=t.getRoot().getSubject()%>
			<br> <font size="5" ><b>Content: </b></font><%=t.getRoot().getContent()%>
			<br><br>
			<button type="button" onclick='showThread(<%=t.getID()%>)'>Quick review</button>
			<br><br>
			<%
			
				out.println("<font size=\"5\" ><a href=\"viewThreadsProcessor?do=show&id="
					+ t.getID()	+ "\">Full review</font></a>");
				if (t.getAuthor().equalsIgnoreCase(username)
							|| (isModerator != null && isModerator.booleanValue()))
						out.println("&nbsp &nbsp &nbsp <a href=\"viewThreadsProcessor?do=delete&id="
								+ t.getID()
								+ "&author="
								+ t.getAuthor()
								+ "\">Delete</a>");
			%>
		</p>
	<div id="thread<%=t.getID()%>"></div>
	<%
		}
	%>
	<br>
	<hr>
	<p align="center">
		<%
			out.println("<font size =\"5\"><a href=\"viewThreadsProcessor?do=add\">Add a new thread</a></font>");
		%>
		<br> <br>
		<font color="Navy" size ="5">
		<a size=5 href="viewFriends">Show friends</a> <br> <a
			href="viewForums">Back to Forums</a> <br> <a size=5 href="logout">logout</a></font>
	</p>
</font>
</body>
</html>